home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_100 / 108_01 / ccrl.c < prev    next >
Text File  |  1985-11-13  |  8KB  |  361 lines

  1.  
  2. #define DIRSTART        0x0     /*CRL directory*/
  3. #define DIREND          0x1ff   /*dir. stops here*/
  4. #define FNCSTART        0x205   /*fnc.'s can start*/
  5. #define TPAHBRLC        0x1     /*hi byte of tpa add*/
  6.  
  7. #define JUMPINSTR       0xc3    /*jump instr. op code*/
  8. #define MASK7           0x7f    /*seven bit mask*/
  9. #define NULENTRY        0x80    /*null function*/
  10.  
  11. #define CR              0xd     /*carriage return*/
  12. #define LF              0xa     /*line feed*/
  13.  
  14. #define MXFNSIZE        4096    /*max function size*/
  15. #define MXRELOCS        512     /*mx # reloc. symbls*/
  16. #define MXRLCJTS        128     /*max # j.t. relocs*/
  17.  
  18.  
  19. char    fncbuf[134], extbuf[134], outbuf[134];
  20. char    filestr[20];            /*where file names are kept*/
  21.  
  22. main(argc, argv)
  23.   int argc;
  24.   char **argv;
  25. {
  26.   char  function[MXFNSIZE];
  27.   int   fncfd, extfd, outfd;
  28.   int   addr, fsize, size, fend, outbytes;
  29.   int   rlc[MXRELOCS], rlcjt[MXRLCJTS];
  30.   int   fip, rip, ripjt, rlcoffset;
  31.   char  *s, *fn;
  32.   int   i, j, k, ip, bust, c, cp;
  33.  
  34.   ip = 0;
  35.  
  36.   printf("Ccrl, 11/19/79\n");
  37.   if(argc < 2)
  38.     { printf("File required\n");
  39.       exit();
  40.     }
  41.  
  42.   s = *++argv;
  43.   fn = filename(s, "COM");
  44.   fncfd = fopen(fn, fncbuf);
  45.   if(fncfd <= 0)
  46.     { printf("Cannot open %s\n",fn);
  47.       exit();
  48.     }
  49.  
  50.   fn = filename(s, "EXT");
  51.   extfd = fopen(fn, extbuf);
  52.   if(extfd <= 0)
  53.     { printf("Cannot open %s\n",fn);
  54.       exit();
  55.     }
  56.  
  57.   fn = filename(s, "CRL");
  58.   outfd = fcreat(fn, outbuf);
  59.   if(outfd <= 0)
  60.     { printf("Cannot create %s\n",fn);
  61.       exit();
  62.     }
  63.   else
  64.     printf("Creating %s\n",fn);
  65.  
  66.   outbytes = -1;
  67.   addr = FNCSTART;
  68.   fn = funcname(extbuf);
  69.   if(fn == '\0')
  70.     { printf("Error: no function name on first line of .ext file\n");
  71.       exit();
  72.     }
  73.   putstr(fn, outbuf, &outbytes);
  74.   putw(addr, outbuf);
  75.   outbytes += 2;
  76.   putc(NULENTRY, outbuf);
  77.   ++outbytes;
  78.   fsize = rdhexlf(extbuf);      /*read function length*/
  79.   size = rdhexlf(extbuf);       /*size incl. all other stuff*/
  80.   fend = addr + size;
  81.   putw(fend, outbuf);
  82.   outbytes += 2;
  83.   printf("Total size is %0d bytes\n", size);
  84.   if(outbytes > DIREND)
  85.     { printf("Error: new directory overflows directory area\n");
  86.       exit();
  87.     }
  88.   while(outbytes < (addr - 1))
  89.     { putc(0,outbuf);
  90.       ++outbytes;
  91.     }
  92.   ip = 0;                       /*count of external functions*/
  93.   do
  94.     { fn = funcname(extbuf);
  95.       if(fn != '\0')
  96.         { while(*fn != '\0')
  97.             { putc(*fn++, outbuf);
  98.               ++outbytes;
  99.             }
  100.           ++ip;
  101.         }
  102.     }
  103.   while(fn != '\0');
  104.   putc(0x0, outbuf);
  105.   ++outbytes;             /*null to end*/
  106.   if(ip > 0)
  107.     { if(3*ip >= 0xff)
  108.         { printf("Error: to many external functions\n");
  109.           exit();
  110.         }
  111.       function[0] = JUMPINSTR;
  112.       function[1] = 3*(ip + 1);
  113.       function[2] = TPAHBRLC;
  114.       fip = 3;
  115.       for(j = 1; j <= ip; j++)
  116.         { function[fip++] = JUMPINSTR;
  117.           function[fip++] = 0x0;
  118.           function[fip++] = 0x0;
  119.         }
  120.       rlcoffset = 3*(ip + 1);
  121.       rlcjt[0] = 1;             /*re-locate jump over jump table*/
  122.       ripjt = 1;                /*this is first relocation parameter*/
  123.     }
  124.   else
  125.     { fip = 0;
  126.       rlcoffset = 0;
  127.       ripjt = 0;
  128.     }
  129.   for(j = 1; j <= fsize; j++)
  130.     { c = getc(fncbuf);
  131.       function[fip++] = c;
  132.       if(fip > MXFNSIZE)
  133.         { printf("Error: function buffer size exceeded\n");
  134.           exit();
  135.         }
  136.     }
  137.   putw(fip, outbuf);
  138.   outbytes += 2;                /*fnc size*/
  139.   if(ripjt)                     /*no JT RLC's unless one already*/
  140.     { while((c = readhex(extbuf))  >=  0)
  141.         { rlcjt[ripjt++] = rlcoffset + c;
  142.           if(ripjt > MXRLCJTS)
  143.             { printf("Error: To many jump table relocation parameters\n");
  144.               exit();
  145.             }
  146.         }
  147.     }
  148.   rip = 0;
  149.   do
  150.     { if((c = readhex(extbuf)) >= 0)
  151.         rlc[rip++] = rlcoffset + c;
  152.       if(rip > MXRELOCS)
  153.         { prinxf("
  154.             else{
  155.                 tabs--;
  156.                 puts();
  157.                 tabs++;
  158.             }
  159.             if((peek = getchr()) == ';'){
  160.                 printf(";");
  161.                 peek = -1;
  162.             }
  163.             getnl();
  164.             puts();
  165.             printf("\n");
  166.             sflg = 1;
  167.             break;
  168.         case '/':
  169.             string[j++] = c;
  170.             if((peek = getchr()) != '*')break;
  171.             string[j++] = peek;
  172.             peek = -1;
  173.             comment();
  174.             break;
  175.         case ')':
  176.             paren--;
  177.             string[j++] = c;
  178.             puts();
  179.             if(getnl() == 1){
  180.                 peek = '\n';
  181.                 if(paren != 0)aflg = 1;
  182.                 else if(tabs > 0){
  183.                     pflg[level]++;
  184.                     tabs++;
  185.                     ind[level] = 0;
  186.                 }
  187.             }
  188.             break;
  189.         case '#':
  190.             string[j++] = c;
  191.             while((cc = getchr()) != '\n')string[j++] = cc;
  192.             string[j++] = cc;
  193.             sflg = 0;
  194.             puts();
  195.             sflg = 1;
  196.             break;
  197.         case '(':
  198.             string[j++] = c;
  199.             paren++;
  200.             if(lookup(wfor) == 1){
  201.                 while((c = gets()) != ';');
  202.                 ct=0;
  203. cont:
  204.                 while((c = gets()) != ')'){
  205.                     if(c == '(') ct++;
  206.                 }
  207.                 if(ct != 0){
  208.                     ct--;
  209.                     goto cont;
  210.                 }
  211.                 paren--;
  212.                 puts();
  213.                 if(getnl() == 1){
  214.                     peek = '\n';
  215.                     pflg[level]++;
  216.                     tabs++;
  217.                     ind[level] = 0;
  218.                 }
  219.                 break;
  220.             }
  221.             if(lookup(wif) == 1){
  222.                 puts();
  223.                 stabs[clevel][iflev] = tabs;
  224.                 spflg[clevel][iflev] = pflg[level];
  225.                 sind[clevel][iflev] = ind[level];
  226.                 iflev++;
  227.                 ifflg = 1;
  228.             }
  229.         }
  230.     }
  231.     if (outvect != 1){
  232.         printf("\032");
  233.         fflush(outbuf);
  234.     }
  235.     exit();
  236. }
  237. ptabs(){
  238.     int i;
  239.     for(i=0; i < tabs; i++)printf("\t");
  240. }
  241. getchr(){
  242.     if(peek < 0 && lastchar != ' ' && lastchar != '\t')pchar = lastchar;
  243.     lastchar = (peek<0) ? getc(inpbuf):peek;
  244.     peek = -1;
  245.     return(lastchar == '\r' ? getchr():lastchar);
  246. }
  247. puts(){
  248.     if(j > 0){
  249.         if(sflg != 0){
  250.             ptabs();
  251.             sflg = 0;
  252.             if(aflg == 1){
  253.                 aflg = 0;
  254.                 if(tabs > 0)printf("    ");
  255.             }
  256.         }
  257.         string[j] = '\0';
  258.         printf("%s",string);
  259.         j = 0;
  260.     }
  261.     else{
  262.         if(sflg != 0){
  263.             sflg = 0;
  264.             aflg = 0;
  265.         }
  266.     }
  267. }
  268. lookup(tab)
  269. char *tab[];
  270. {
  271.     char r;
  272.     int l,kk,k,i;
  273.     if(j < 1)return(0);
  274.     kk=0;
  275.     while(string[kk] == ' ')kk++;
  276.     for(i=0; tab[i] != 0; i++){
  277.         l=0;
  278.         for(k=kk;(r = tab[i][l++]) == string[k] && r != '\0';k++);
  279.         if(r == '\0' && (string[k] < 'a' || string[k] > 'z'))return(1);
  280.     }
  281.     return(0);
  282. }
  283. gets(){
  284.     char ch;
  285. beg:
  286.     if((ch = string[j++] = getchr()) == '\\'){
  287.         string[j++] = getchr();
  288.         goto beg;
  289.     }
  290.     if(ch == '\'' || ch == '"'){
  291.         while((cc = string[j++] = getchr()) != ch)if(cc == '\\')string[j++] = getchr();
  292.         goto beg;
  293.     }
  294.     if(ch == '\n'){
  295.         puts();
  296.         aflg = 1;
  297.         goto beg;
  298.     }
  299.     else return(ch);
  300. }
  301. gotelse(){
  302.     tabs = stabs[clevel][iflev];
  303.     pflg[level] = spflg[clevel][iflev];
  304.     ind[level] = sind[clevel][iflev];
  305.     ifflg = 1;
  306. }
  307. getnl(){
  308.     while((peek = getchr()) == '\t' || peek == ' '){
  309.         string[j++] = peek;
  310.         peek = -1;
  311.     }
  312.     if((peek = getchr()) == '/'){
  313.         peek = -1;
  314.         if((peek = getchr()) == '*'){
  315.             string[j++] = '/';
  316.             string[j++] = '*';
  317.             peek = -1;
  318.             comment();
  319.         }
  320.         else string[j++] = '/';
  321.     }
  322.     if((peek = getchr()) == '\n'){
  323.         peek = -1;
  324.         return(1);
  325.     }
  326.     return(0);
  327. }
  328. comment(){
  329. rep:
  330.     while((c = string[j++] = getchr()) != '*')
  331.         if(c == '\n'){
  332.             puts();
  333.             sflg = 1;
  334.         }
  335. gotstar:
  336.     if((c = string[j++] = getchr()) != '/'){
  337.         if(c == '*')goto gotstar;
  338.         goto rep;
  339.     }
  340. }
  341. printf(format,arg)
  342. char *format;
  343. unsigned arg;
  344. {
  345.     if (fprintf(outvect,format,arg) == -1){
  346.         fprintf(1,"Disk write error\n");
  347.         exit();
  348.     }
  349. }
  350. ÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷